package com.tgpms.interfaces.dto;

import com.tgpms.common.Result;
import com.tgpms.interfaces.Request.HttpClient;
import com.tgpms.interfaces.common.CommonRequestDispose;
import com.tgpms.interfaces.properties.Url;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * Created By Riven on 2020-10-14
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
@Slf4j
public class Portal implements PortalInterface {

    @Override
    public Result parsingOaToken(String clientId, String clientSecret, String code, String grantType) {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        params.put("client_id", clientId);
        params.put("client_secret", clientSecret);
        params.put("code", code);
        params.put("grant_type", grantType);
        System.out.println("访问连接地址：" + Url.PORTAL_TOKEN);
        System.out.println("传递参数====>>>>" + params);
        String datas = new HttpClient().post(Url.PORTAL_TOKEN, params, HttpMethod.POST);
        return dispose.commonDispose(datas);
    }

    @Override
    public Result getUserInfo(String accessToken, String clientId, String uid) {
        CommonRequestDispose dispose = new CommonRequestDispose();
        Map<String, Object> params = new HashMap<>(16);
        params.put("client_id", clientId);
        params.put("access_token", accessToken);
        params.put("uid", uid);
        System.out.println("访问连接地址：" + Url.PORTAL_USERINFO);
        System.out.println("传递参数====>>>>" + params);
        String datas = new HttpClient().post(Url.PORTAL_USERINFO, params, HttpMethod.POST);
        return dispose.commonDispose(datas);
    }
}
